package com.example.chatroom.controller;

import com.example.chatroom.common.AjaxResult;
import com.example.chatroom.common.ApplicationVariable;
import com.example.chatroom.common.PasswordTools;
import com.example.chatroom.entity.UserEntity;
import com.example.chatroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // todo: 用户注册功能
    @RequestMapping("/register")
    public AjaxResult reg(UserEntity userEntity){
        // 1，进行非空校验
        if(userEntity==null || !StringUtils.hasLength(userEntity.getUsername())
        || !StringUtils.hasLength(userEntity.getPassword()))
            return AjaxResult.fail(-1,"参数有误！");
        UserEntity user = userService.selectUserByName(userEntity.getUsername());
        if(user != null)
            return AjaxResult.fail(-2,"用户名已存在！");
        // todo: 将密码进行加盐加密操作
        userEntity.setPassword(PasswordTools.encrypt(userEntity.getPassword()));
        // 2,调用UserService执行添加方法，并将返回的结果添加至AjaxResult.data中
        int result = userService.reg(userEntity);
        return AjaxResult.success(result);
    }

    // todo: 用户登录功能
    @RequestMapping("/login")
    public AjaxResult login(String username, String password, HttpServletRequest request){
        if(!StringUtils.hasLength(username)
                || !StringUtils.hasLength(password))
            return AjaxResult.fail(-1,"参数有误！");
        UserEntity userEntity = userService.selectUserByName(username);
        if(userEntity == null || userEntity.getUser_id() < 0)
            return AjaxResult.fail(-3,"用户名或密码错误！");
        // todo: 验证密码是否正确
        if(!PasswordTools.decrypt(password,userEntity.getPassword()))
            return AjaxResult.fail(-3,"用户名或密码错误！");
//        if(!userEntity.getPassword().equals(password))
//            return AjaxResult.fail(-3,"用户名或密码错误！");
        // todo: 将当前登录的用户信息存储到Session
        HttpSession session = request.getSession(true);
        session.setAttribute(ApplicationVariable.SESSION_USER_KEY,userEntity);
        return AjaxResult.success(1);
    }


    @RequestMapping ("/userInfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        // 1. 先从请求中获取到会话
        HttpSession session = request.getSession(false);
        if (session == null) {
            // 会话不存在, 用户尚未登录.
            System.out.println("[getUserInfo] 当前获取不到 session 对象!");
            return AjaxResult.fail(-1,"session对象获取不到",new UserEntity());
        }
        // 2. 从会话中获取到之前保存的用户对象.
        UserEntity user = (UserEntity) session.getAttribute(ApplicationVariable.SESSION_USER_KEY);
        if (user == null) {
            System.out.println("[getUserInfo] 当前获取不到 user 对象!");
            return AjaxResult.fail(-1,"user对象获取不到",new UserEntity());
        }
        user.setPassword("");
        return AjaxResult.success(user);
    }
}
